#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.

"""Google News Aggregator

Implements the aggregator by grabbing Google News XMLs and uses AppEngine
memcache to reduce the latency.
"""

from datetime import datetime
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.dist import use_library
use_library('django', '1.2')
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from db import user
from libs import login_util
from xml.dom import minidom

import re
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

def getTagText(root, tag):
    node = root.getElementsByTagName(tag)[0]
    rc = ""
    for node in node.childNodes:
        if node.nodeType in (node.TEXT_NODE, node.CDATA_SECTION_NODE):
            rc = rc + node.data
    return rc

def toUTF(str):
    return str.encode('utf-8')

def getCurrentTime():
    return datetime.now()

class MainPage(webapp.RequestHandler):
    def get(self):
        self.GOOGLE_NEWS_ALL = 'google_news_all'
        self.GOOGLE_FUN_ALL = 'google_fun_all'
        self.GOOGLE_WORLD_ALL = 'google_world_all'
        self.GOOGLE_SCI_ALL = 'google_sci_all'
        self.GOOGLE_BUSINESS_ALL = 'google_business_all'
        self.GOOGLE_SPORTS_ALL = 'google_sports_all'
        self.GOOGLE_HEALTH_ALL = 'google_health_all'
        self.GOOGLE_POPULAR_ALL = 'google_popular_all'
        
        self.fetch_map = {self.GOOGLE_NEWS_ALL:'http://news.google.com/news?hdlOnly=1&cf=all&ned=us&hl=en&output=rss&num=30',
                          self.GOOGLE_FUN_ALL:'http://news.google.com/news?cf=all&ned=us&hl=en&topic=e&output=rss&num=30',
                          self.GOOGLE_WORLD_ALL:'http://news.google.com/news?cf=all&ned=us&hl=en&topic=w&output=rss&num=30',
                          self.GOOGLE_SCI_ALL:'http://news.google.com/news?cf=all&ned=us&hl=en&topic=t&output=rss&num=30',
                          self.GOOGLE_BUSINESS_ALL:'http://news.google.com/news?cf=all&ned=us&hl=en&topic=b&output=rss&num=30',
                          self.GOOGLE_SPORTS_ALL:'http://www.google.com/news/section?cf=all&ned=us&topic=s&ict=ln&output=rss&num=30',
                          self.GOOGLE_HEALTH_ALL:'http://www.google.com/news/section?cf=all&ned=us&topic=m&ict=ln&output=rss&num=30',
                          self.GOOGLE_POPULAR_ALL:'http://news.google.com/news?cf=all&ned=us&hl=en&topic=po&output=rss&num=30'}
        self.content_map = {}
        
        self.response.headers['Content-Type'] = 'text/html;charset=UTF-8'
        template_values = {}
        mem_cache = memcache.Client()
        
        for key in self.fetch_map:
            newsList = mem_cache.get(key)
            if not newsList:
                result = urlfetch.Fetch(url=self.fetch_map[key], headers={'user-agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'})
                if result.status_code == 200:
                    newsList = self.parseNewsDom(result.content)
                    mem_cache.add(key=key, value=newsList, time=3600)
            if key == self.GOOGLE_NEWS_ALL:
                template_values['top_news_list'] = newsList
                template_values['top_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_FUN_ALL:
                template_values['fun_news_list'] = newsList
                template_values['fun_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_WORLD_ALL:
                template_values['world_news_list'] = newsList
                template_values['world_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_SCI_ALL:
                template_values['sci_news_list'] = newsList
                template_values['sci_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_BUSINESS_ALL:
                template_values['business_news_list'] = newsList
                template_values['business_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_SPORTS_ALL:
                template_values['sports_news_list'] = newsList
                template_values['sports_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_HEALTH_ALL:
                template_values['health_news_list'] = newsList
                template_values['health_news_date'] = getCurrentTime()
            elif key == self.GOOGLE_POPULAR_ALL:
                template_values['popular_news_list'] = newsList
                template_values['popular_news_date'] = getCurrentTime()
        
        template_values['email'] = None
        if login_util.LoginUtil.IsUserLogin():
            email = login_util.LoginUtil.GetLoginedUserEmail()
            profile = user.GetUserByEmail(email)
            if profile:
                template_values['email'] = email
                template_values['last_name'] = profile.last_name
                template_values['first_name'] = profile.first_name
                template_values['nick_name'] = profile.nick_name
            
                login_git_account = login_util.LoginUtil.GetLoginedGitAccount()
                display_name = profile.first_name + ' ' + profile.last_name

                if not display_name:
                    display_name = login_git_account.GetDisplayName()
                template_values['display_name'] = display_name
                template_values['photo_url'] = login_git_account.GetPhotoUrl()
                if profile.password:
                    template_values['legacy'] = 'true'
                else:
                    template_values['legacy'] = 'false'
                
        template_values['host_url'] = self.request.environ['wsgi.url_scheme'] + '://' + self.request.environ['HTTP_HOST']
        
        path = os.path.join(os.path.dirname(__file__), '../templates/index.tpl')
        self.response.out.write(template.render(path, template_values))
    
    def parseNewsDom(self, content):
        dom = minidom.parseString(content)
        root = dom.documentElement
        items = root.getElementsByTagName('item')
        result = [];
        
        for item in items:
            news = {};
            news['title'] = toUTF(getTagText(item, 'title'))
            news['url'] = toUTF(getTagText(item, 'link'))
            news['pubDate'] = toUTF(getTagText(item, 'pubDate')).replace(' GMT+00:00]', '')
            p = re.compile('<img src="(.*)" alt')
            description = getTagText(item, 'description')
            res = p.findall(description)
            if len(res) > 0:
                news['snapshot'] = res[0]
            else:
                news['snapshot'] = '' 
            result.append(news)
        
        return result

application = webapp.WSGIApplication([('/', MainPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()